import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/component_management_ledger_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/process_nodes_entity.dart';
import 'package:get/get.dart';

///部件管理台账详情
class ComponentManagementLedgerDetailController extends GetxController {
  ComponentManagementLedgerEntity entity = ComponentManagementLedgerEntity();

  /// 是不是审批页面
  bool isApproval = false;

  /// 审核意见
  String comments = '';

  /// 签字
  String signatureAddress = '';

  /// 审批流程数据
  List<ProcessNodesEntity>? nodeList;

  @override
  void onInit() {
    super.onInit();
    if (Get.arguments['isApproval'] != null && Get.arguments['isApproval'] is bool) {
      isApproval = Get.arguments['isApproval'] as bool;
    }
    initData();
    getProcessListData();
  }

  ///初始化页面数据
  Future<void> initData() async {
    final res = await HttpUtil().generateGetRequest<ComponentManagementLedgerEntity>(
      Interface.componentManagementLedgerGet,
      queryParameters: {'id': Get.arguments['id']},
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        entity = result.data!;
      }
    });
    update();
  }

  /// 获取流程列表数据
  void getProcessListData() async {
    // 获取流程列表接口
    final res = await HttpUtil().generateGetRequest<List<ProcessNodesEntity>>(
      Interface.getApprovalHistory,
      queryParameters: {'businessId': Get.arguments['id']},
      // queryParameters: {'businessId': '4fd77c39-8617-4cc0-97eb-558bcbda814c'},
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        nodeList = result.data!;
      }
      update();
    });
  }


  /// 输入回调
  void onOpinion(String value) {
    comments = value;
    update();
  }

  /// 签字回调
  void onSign(String url) {
    signatureAddress = url;
  }

  /// 操作回调
  /// [type]1:通过 2:驳回 驳回即为不通过
  Future<void> onOperation(int type) async {
    final res = await HttpUtil().generatePostRequest(
      Interface.foundationStartApproval,
      data: {
        'businessId': entity.id, //业务数据id
        'reject': type == 2, //是否驳回
        'comments': comments, //	意见
        'code': 2, //	页面编号：1.设备设施管理 2.部件管理 3：仪表连锁摘除
        'signatureAddress': signatureAddress //签名地址
      },
    ).execute();
    res.onSuccess((result) {
      Get.back(closeOverlays: true, result: true);
      AppToast.showSuccess('执行成功!');
    });
  }
}
